package app.qrcode.browser.ykpixabay.ui.star

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import app.qrcode.browser.ykpixabay.R
import app.qrcode.browser.ykpixabay.logic.model.Hits
import kotlinx.android.synthetic.main.fragment_star.*

/**
 * Created by YiGo on 2022/4/21.
 */
class StarFragment : Fragment() {

    private val adapter by lazy { StarAdapter(this) }

    val starViewModel by lazy { ViewModelProvider(this).get(StarViewModel::class.java) }

    private var starList = mutableListOf<Hits>()

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_star, container, false)
    }


    @SuppressLint("NotifyDataSetChanged")
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        recyclerview_star.also {
            it.layoutManager = LinearLayoutManager(context)
            it.adapter = adapter
        }
        button_chooseall.visibility = View.GONE
        button_delete.visibility = View.GONE
        starViewModel.dataListLive.observe(viewLifecycleOwner, Observer {
            starViewModel.change(it, starList)
            adapter.updateStarList(starList)
        })
        button_edit.isSelected = false
        button_edit.setOnClickListener {
            it.isSelected = !it.isSelected
            if (it.isSelected) {
                button_chooseall.visibility = View.VISIBLE
                button_delete.visibility = View.VISIBLE
                button_edit.text = "完成"
                adapter.editState = 1
                adapter.notifyDataSetChanged()
            } else {
                button_chooseall.visibility = View.GONE
                button_delete.visibility = View.GONE
                button_edit.text = "编辑"
                adapter.editState = 0
                adapter.notifyDataSetChanged()
            }

        }

        button_delete.setOnClickListener {
            adapter.deleteObjectIdHashMap.forEach {
                starViewModel.deleteOneItem(it.value)
            }
            adapter.editState = 0
            button_delete.visibility = View.GONE
            button_chooseall.visibility = View.GONE
            button_edit.text = "编辑"
            adapter.notifyDataSetChanged()
        }
        button_chooseall.isSelected = false
        button_chooseall.setOnClickListener {
            it.isSelected = !it.isSelected
            if (it.isSelected) {
                button_chooseall.text = "取消全选"
                adapter.chooseAllState = 1
                adapter.chooseAll()
                adapter.notifyDataSetChanged()
            } else {
                button_chooseall.text = "全选"
                adapter.chooseAllState = 0
                adapter.cancelChooseAll()
                adapter.notifyDataSetChanged()
            }
        }
    }
}